

'-------------------------------------------------------------
' Hands-On 12-1
' Statements to be entered in the Immediate Window.
'-------------------------------------------------------------

?CurDir
?CurDir("D:\")
myDrive = Left(CurDir$,1)
?myDrive
myDrive = Left(CurDir$,2)
?myDrive


'-------------------------------------------------------------
' Hands-On 12-2
' Statements to be entered in the Immediate Window.
' System responses are not shown here
'-------------------------------------------------------------

?Dir("C:\", vbNormal)
?Dir
mfile = Dir("C:\", vbHidden)
?mfile
mfile = Dir
?mfile
mfile = Dir
?mfile
If Dir("C:\stamp.bat") = "" Then Debug.Print "File was not found."
If Dir ("C:\autoexec.bat") <> "" Then Debug.Print "This file exists on your C drive."

?Dir("C:\WINDOWS\*.ini", vbNormal)

?dir

?dir

?dir

?dir

?dir


'-------------------------------------------------------------
' Hands-On 12-3
'-------------------------------------------------------------


Sub MyFiles()
    Dim mfile As String
    Dim mpath As String

    mpath = InputBox("Enter pathname, e.g. C:\Excel")
    If Right(mpath, 1) <> "\" Then mpath = mpath & "\"

    mfile = Dir(mpath & "*.*")
    If mfile <> "" Then Debug.Print "Files in the " & mpath & " folder:"
    Debug.Print LCase$(mfile)
    If mfile = "" Then
        MsgBox "No files found."
        Exit Sub
    End If
    Do While mfile <> ""
        mfile = Dir
        Debug.Print LCase$(mfile)
    Loop
End Sub


Sub GetFiles()
    Dim myFile As String
    Dim nextRow As Integer

    nextRow = 1
    With Worksheets("Sheet1").Range("A1")
        myFile = Dir("C:\", vbNormal)
        .Value = myFile
        Do While myFile <> ""
            myFile = Dir
            .Offset(nextRow, 0).Value = myFile
            nextRow = nextRow + 1
        Loop
    End With
End Sub



'-------------------------------------------------------------
' Hands-On 12-4
' Statements to be entered in the Immediate Window.
'-------------------------------------------------------------

?FileDateTime("C:\Config.sys")
?DateValue(FileDateTime("C:\Config.sys"))
?TimeValue(FileDateTime("C:\Config.sys"))
If DateValue(FileDateTime("C:\Config.sys")) < Date then Debug.Print "This file was not modified today."


'-------------------------------------------------------------
' Hands-On 12-5
'-------------------------------------------------------------

Sub TotalBytesIni()
    Dim iniFile As String
    Dim allBytes As Long

    iniFile = Dir("C:\WINDOWS\*.ini")
    allBytes = 0
    Do While iniFile <> ""
        allBytes = allBytes + FileLen("C:\WINDOWS\" & iniFile)
        iniFile = Dir
    Loop
    Debug.Print "Total bytes: " & allBytes
End Sub


'-------------------------------------------------------------
' Hands-On 12-6
'-------------------------------------------------------------

' Statements to be entered in the Immediate Window

?getattr("C:\MsDos.sys") And vbReadOnly
?getattr("C:\MsDos.sys") And vbHidden
?getattr("C:\MsDos.sys") And vbSystem
?getattr("C:\MsDos.sys") And vbArchive


Sub GetAttributes()
    Dim attr As Integer
    Dim msg As String

    attr = GetAttr("C:\MSDOS.SYS")
    msg = ""

    If attr And vbReadOnly Then msg = msg & "Read-Only (R)"
    If attr And vbHidden Then msg = msg & Chr(10) & "Hidden (H)"
    If attr And vbSystem Then msg = msg & Chr(10) & "System (S)"
    If attr And vbArchive Then msg = msg & Chr(10) & "Archive (A)"
    MsgBox msg, , "MSDOS.SYS"
End Sub

'-------------------------------------------------------------
' Hands-On 12-7
' Statements to be entered in the Immediate Window
'-------------------------------------------------------------

SetAttr "C:\stamps.txt", vbReadOnly + vbHidden
?GetAttr("C:\stamps.txt")


'-------------------------------------------------------------
' Hands-On 12-8
' Statements to be entered in the Immediate Window
'-------------------------------------------------------------

MkDir "C:\Mail"
ChDir "C:\Mail"
?CurDir
ChDir "C:\"
RmDir "C:\Mail"


'-------------------------------------------------------------
' Hands-On 12-9
'-------------------------------------------------------------

Sub CopyToAbortFolder()
    Dim folder As String
    Dim source As String
    Dim dest As String
    Dim msg1 As String
    Dim msg2 As String
    Dim p As Integer
    Dim s As Integer
    Dim i As Long

    On Error GoTo ErrorHandler

    folder = "C:\Abort"
    msg1 = "The selected file is already in this folder."
    msg2 = "was copied to"
    p = 1
    i = 1
    ' get the name of the file from the user
    source = Application.GetOpenFilename
    ' don't do anything if cancelled
    If source = "False" Then Exit Sub
    ' get the total number of backslash characters "\" in the source 
    ' variable's contents
    Do Until p = 0
        p = InStr(i, source, "\", 1)
        If p = 0 Then Exit Do
        s = p
        i = p + 1
    Loop
    ' create the destination file name
    dest = folder & Mid(source, s, Len(source))
        ' create a new folder with this name
        MkDir folder
        ' check if the specified file already exists in the
        ' destination folder
        If Dir(dest) <> "" Then
            MsgBox msg1
        Else
        ' copy the selected file to the C:\Abort folder
            FileCopy source, dest
            MsgBox source & " " & msg2 & " " & dest
        End If
        Exit Sub
ErrorHandler:
        If Err = "75" Then
            Resume Next
        End If
        If Err = "70" Then
            MsgBox "You can't copy an open file."
        Exit Sub
    End If
End Sub


'-------------------------------------------------------------
' Hands-On 12-10
'-------------------------------------------------------------

Sub RemoveMe()
    Dim folder As String
    Dim myFile As String

    ' assign the name of folder to the folder variable
    ' notice the ending backslash "\"
    folder = "C:\Abort\"
    myFile = Dir(folder, vbNormal)

    Do While myFile <> ""
        Kill folder & myFile
        myFile = Dir
    Loop
        RmDir folder
End Sub

